/*
 This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. 
 To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or 
 send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 
 Jed Laudenslayer
 http://kwigbo.com
 
 */


#import <SystemConfiguration/SCNetworkReachability.h>
#include <netinet/in.h>

#import "SSDataManager.h"
#import "SSLevel.h"

#import "SSEntityController.h"
#import "WolfEntityView.h"

#import "SSEntity.h"


@interface SSDataManager (Private)
- (void)createLevel1;
- (void)createLevel2;
- (void)createLevel3;

- (void)createLevels;

@end

@implementation SSDataManager

@synthesize currentLevel, levels, currentIndex, misses, score, missed, defaults;

- (void) dealloc
{
	[defaults release];
	[levels release];
	[super dealloc];
}

-(id) init
{
	self = [super init];
	
	if (self)
	{
		defaults = [NSUserDefaults standardUserDefaults];
		
		[self createLevels];
	}
	
	return self;
}

#pragma mark -
#pragma mark create levels


- (void) createLevels
{	//Perfect 378
	CMLog(@"in createLevels");
	
	self.levels = [[NSMutableArray alloc] init];
	[self createLevel1];
	//[self createLevel2];
	//[self createLevel3];
	CMLog(@"end in createLevels");
	
}

- (void)createLevel1{
	SSLevel *level = [SSLevel node];
	level.background = [CCSprite spriteWithFile:@"bg1.png"];
	
	level.playerInitialLocation = CGPointMake(395, 120);
	[level initEmitterSnowWithTotalParticles:50];
	
	SSEntityController *controller1 = [[SSEntityController alloc] init];
	WolfEntityView *wolf1 = [[WolfEntityView alloc] initWithBalloonColor:@"green"];
	controller1.entityView = wolf1;
	wolf1.controller = controller1;
	[controller1.entityModel.stats setHp:1];
	[controller1.entityModel.stats setMaxHp:1];
	[controller1.entityModel setPointPosition:LEFT];
	
	SSEntityController *controller2 = [[SSEntityController alloc] init];
	WolfEntityView *wolf2 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	controller2.entityView = wolf2;
	wolf2.controller = controller2;
	[controller2.entityModel.stats setHp:2];
	[controller2.entityModel.stats setMaxHp:2];
	[controller2.entityModel setPointPosition:LEFT];
	
	SSEntityController *controller3 = [[SSEntityController alloc] init];
	WolfEntityView *wolf3 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	controller3.entityView = wolf3;
	wolf3.controller = controller3;
	[controller3.entityModel.stats setHp:2];
	[controller3.entityModel.stats setMaxHp:2];
	[controller3.entityModel setPointPosition:LEFT];
	
	//WolfEntityView *wolf2 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf3 = [[WolfEntityView alloc] initWithBalloonColor:@"red"];
//	WolfEntityView *wolf4 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf5 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf6 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf7 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf8 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf9 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
//	WolfEntityView *wolf10 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	
	//level.enemies = [[NSMutableArray arrayWithObjects:wolf1, wolf2, wolf3, wolf4, wolf5, wolf6, wolf7, wolf8, wolf9, wolf10, nil] retain];
	level.enemies = [[NSMutableArray arrayWithObjects:controller1, controller2, controller3, nil] retain];
//	level.items = ;
	
	[self.levels addObject:level];
}

- (void)createLevel2{
	SSLevel *level = [SSLevel node];
	level.background = [CCSprite spriteWithFile:@"bg2.png"];
	
	level.playerInitialLocation = CGPointMake(395, 120);
	[level initEmitterRainWithTotalParticles:-1];
	
	WolfEntityView *wolf1 = [[WolfEntityView alloc] initWithBalloonColor:@"green"];
	WolfEntityView *wolf2 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf3 = [[WolfEntityView alloc] initWithBalloonColor:@"red"];
	WolfEntityView *wolf4 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf5 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf6 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf7 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf8 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf9 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf10 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	
	level.enemies = [[NSMutableArray arrayWithObjects:wolf1, wolf2, wolf3, wolf4, wolf5, wolf5, wolf6, wolf7, wolf8, wolf9, wolf10, nil] retain];
	
	[self.levels addObject:level];
}

- (void)createLevel3{
	SSLevel *level = [SSLevel node];
	level.background = [CCSprite spriteWithFile:@"back3.png"];
	
	level.playerInitialLocation = CGPointMake(395, 120);
	[level initEmitterRainWithTotalParticles:-1];
	
	WolfEntityView *wolf1 = [[WolfEntityView alloc] initWithBalloonColor:@"green"];
	WolfEntityView *wolf2 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf3 = [[WolfEntityView alloc] initWithBalloonColor:@"red"];
	WolfEntityView *wolf4 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf5 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf6 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf7 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf8 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf9 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	WolfEntityView *wolf10 = [[WolfEntityView alloc] initWithBalloonColor:@"yellow"];
	
	level.enemies = [[NSMutableArray arrayWithObjects:wolf1, wolf2, wolf3, wolf4, wolf5, wolf5, wolf6, wolf7, wolf8, wolf9, wolf10, nil] retain];
	
	[self.levels addObject:level];
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Singleton ///////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

static SSDataManager *sharedDataManager = nil;

+ (SSDataManager *) sharedManager
{
    @synchronized(self)
	{
        if (sharedDataManager == nil)
		{
            [[self alloc] init];
        }
    }
	
    return sharedDataManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
	{
        if(sharedDataManager == nil)
		{
            sharedDataManager = [super allocWithZone:zone];
            return sharedDataManager;
        }
    }
	
    return nil;
}

- (id)copyWithZone:(NSZone *)zone
{	
    return self;
}

- (id) retain
{
    return self;
}

- (unsigned) retainCount
{
    return UINT_MAX;
}

- (void)release
{
	
}

- (id) autorelease
{
    return self;
}

@end
